package org.ricks.ioc;

import org.ricks.common.conf.ClassHandler;
import org.ricks.common.conf.ConfStarter;
import org.ricks.ioc.bean.factory.AbstractBeanFactory;
import org.ricks.utils.PackageUtils;
import java.util.List;
import java.util.Set;

/**
 * @author chenwei
 * @Title:
 * @Package
 * @Description:
 * @date 2020/8/817:21
 */
public abstract class AbstractAppContext implements App {

    protected Set<Class<?>> classes;

    protected AbstractBeanFactory beanFactory;

    public AbstractAppContext(AbstractBeanFactory beanFactory, String packagePath) {
        this.beanFactory = beanFactory;
        classes = PackageUtils.getClasses(packagePath);
        ClassHandler.getClassHandler().addClassSet(classes);
        ConfStarter.start();
    }

    public void refresh() {
        loadBeanDefinitions(beanFactory);
        beanFactory.instanceBeans();
    }

    protected abstract void loadBeanDefinitions(AbstractBeanFactory beanFactory);

    @Override
    public Object getBean(String name) {
        return beanFactory.getBean(name);
    }

    @Override
    public Object getBean(Class clazz) {
        return beanFactory.getBean(clazz);
    }

    public List<Object> getBeans() {
        return beanFactory.getBeans();
    }

    @Override
    public AbstractBeanFactory getBeanFactory() {
        return beanFactory;
    }
}
